package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.S;
import com.example.demo.dto.DishDto;
import com.example.demo.entity.Category;
import com.example.demo.entity.Dish;

import com.example.demo.entity.DishFlavor;
import com.example.demo.service.CategoryService;
import com.example.demo.service.DishFlavorService;
import com.example.demo.service.DishService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    //新增菜品
    @PostMapping
    public S<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return S.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public S<Page> page(int page,int pageSize,String name){
        //构造分页构造器对象
        Page<Dish> page1=new Page<>(page,pageSize);
        Page<DishDto> page2=new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        //分页查询
        dishService.page(page1,lambdaQueryWrapper);
        //拷贝对象
        BeanUtils.copyProperties(page1,page2,"records");
        List<Dish> records = page1.getRecords();
        List<DishDto> collect = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            log.info("DishDto"+dishDto.toString());

            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                dishDto.setCategoryName(byId.getName());
                log.info("更具id查询{}"+byId.getName());
            }
            //dishDto.setCategoryName(byId.getName());
            return dishDto;
        }).collect(Collectors.toList());
        page2.setRecords(collect);
        log.info(String.valueOf(page2));
        return S.success(page2);
    }
    //根据id查询菜品对应的信息和口味信息
    @GetMapping("/{id}")
    public S<DishDto> get(@PathVariable Long id){
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);
        return S.success(byIdWithFlavor);
    }
    //修改菜品
    @PutMapping
    public S<String> update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return S.success("添加成功");
    }
    //根据菜品id查询菜品对应的信息和口味信息
    @GetMapping("/list")
    public S<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        //添加条件，状态查询为1的菜品
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        List<DishDto> list1=new ArrayList<>();
        list1=list.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            log.info("DishDto"+dishDto.toString());

            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                dishDto.setCategoryName(byId.getName());
                log.info("更具id查询{}"+byId.getName());
            }
            //dishDto.setCategoryName(byId.getName());
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list2 = dishFlavorService.list(lambdaQueryWrapper1);
            dishDto.setFlavors(list2);
             return dishDto;
        }).collect(Collectors.toList());
        return S.success(list1);
    }
    //根据菜品id查询菜品对应的信息和口味信息
//    @GetMapping("/list")
//    public S<List<Dish>> list(Dish dish){
//        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
//        //添加条件，状态查询为1的菜品
//        lambdaQueryWrapper.eq(Dish::getStatus,1);
//        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        List<Dish> list = dishService.list(lambdaQueryWrapper);
//        return S.success(list);
//    }
}
